/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proliferacion;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Systems
 */
public class AccesoBDProliferacion {

    private Connection connection;
    private Statement statement;

    public AccesoBDProliferacion() {
        // load the sqlite-JDBC driver using the current class loader
        try {
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
        connection = null;
    }

    public void conectar() throws SQLException {
        connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Systems\\Desktop\\Conexion.sqlite");

        //connection = DriverManager.getConnection("jdbc:sqlite:..\\Conexion.sqlite");
        if (connection == null) {
            System.out.println("Mala conexion en conectar");
        }
    }

    public void desconectar() throws SQLException {
        if (connection != null) {
            connection.close();
        }
    }

    //ingresando lo datos de la tabla
    /*
     * statement.executeUpdate("drop table if exists Celula");
     * statement.executeUpdate("create table Celula (id_Celula integer, Lado
     * integer, Vertices integer, Cara String)");
     */
    public int addCelula(proliferacion.logica.Celula c) {
        try {
            this.conectar();
            if (connection == null) {
                System.out.println("Mala conexion en add.");
            }
            System.out.println("Vamos a ingresar datos en la tabla celula");
            
            statement = connection.createStatement();
            statement.setQueryTimeout(30);  // set timeout to 30 sec.
            int rs = statement.executeUpdate("INSERT INTO CELULAS VALUES (" + c.getIdCell() + "," + c.getNumVertices() + "," + c.getNumVertices() + ",'Nada')");

            this.desconectar();
            System.out.println("Acabamos de ingresar datos en la tabla celula    " + rs);
            return rs;
        } catch (SQLException e) {
            // if the error message is "out of memory", 
            // it probably means no database file is found
            System.err.println(e.getMessage());
            return 0;
        }
    }

    public proliferacion.logica.Celula getCelula(int idCel) {

        proliferacion.logica.Celula resultado = null;

        try {
            this.conectar();
            statement = connection.createStatement();
            statement.setQueryTimeout(30);  // set timeout to 30 sec.
            ResultSet rs = statement.executeQuery("select * from Celulas where id_Celula=" + idCel);

            while (rs.next()) {
                // read the result set
                System.out.println("id_Celula = " + rs.getString(1));
                resultado = new proliferacion.logica.Celula(rs.getInt(1), rs.getInt(2));

            }
        } catch (SQLException e) {
            // if the error message is "out of memory", 
            // it probably means no database file is found
            System.err.println(e.getMessage());

        } catch (Exception e) {
            // if the error message is "out of memory", 
            // it probably means no database file is found
            System.err.println(e.getMessage());
        } finally {
            try {
                this.desconectar();
            } catch (SQLException e) {
                // if the error message is "out of memory", 
                // it probably means no database file is found
                System.err.println(e.getMessage());
            }
        }
        return resultado;
    }
}
